﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;
using System.Text.RegularExpressions;

namespace Interface.Users
{
    public partial class EditUser : System.Web.UI.Page
    {
        DataConnection dc = new DataConnection();
        StoreProcedures sp = new StoreProcedures();
        string stringID;
        protected void Page_Load(object sender, EventArgs e)
        {
            loadDate();
            loadMonth();
            loadYear();
            if (Page.IsPostBack == false)
            {
                getQueryString();
            }
        }

        public void getQueryString()
        {
            if (Request.QueryString["ID"] != null)
            {
                stringID = Request.QueryString["ID"].ToString();
                bindUser(stringID);
            }
        }

        protected void bindUser(string userID)
        {
            string sqlUser = "Select * from Users where UserID='"+userID+"'";
            DataTable dt = dc.getData(sqlUser);
            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                
                string fullName = row["Fullname"].ToString();
                string email = row["Email"].ToString();
                string birthday = row["Birthday"].ToString();
                string gender = row["Gender"].ToString();
                string memberType = row["MemberType"].ToString();                
                string phoneNo = row["PhoneNo"].ToString();
                string add = row["Address"].ToString();

                hdfID.Value = userID;
                txtNameEdit.Text = fullName;
                txtEmailEdit.Text = email;
                ddlGenderEdit.Text = gender;
                ddlMBTEdit.Text = memberType;
                txtPhoneNoEdit.Text = phoneNo;
                txtAddEdit.Text = add;

            }
        }

        protected void loadDate()
        {
            for (int i = 1; i <= 31; i++)
            {
                ddlDate.Items.Add(i.ToString());
            }
        }

        protected void loadMonth()
        {
            for (int i = 1; i <= 12; i++)
            {
                ddlMonth.Items.Add(i.ToString());
            }
        }

        protected void loadYear()
        {
            for (int i = 1900; i <= 2050; i++)
            {
                ddlYear.Items.Add(i.ToString());
            }
        }

        protected void ibtnUpdate_Click(object sender, ImageClickEventArgs e)
        {
            

            Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(txtEmailEdit.Text);
            if (txtEmailEdit.Text == string.Empty || (!(match.Success)))
            {
                lblMs.Text = "Bạn chưa nhập email hoặc email ko hợp lệ";
                return;
            }

            if (txtNameEdit.Text == string.Empty)
            {
                lblMs.Text = "Bạn chưa nhập tên của bạn";
                return;
            }

            string strDate = ddlDate.SelectedItem.Value;
            string strMonth = ddlMonth.SelectedItem.Value;
            string strYear = ddlYear.SelectedItem.Value;
            int currYear = Convert.ToInt32(DateTime.Now.Year);
            int age = currYear - Convert.ToInt32(strYear);
            if (age < 15)
            {
                lblMs.Text = "Tuổi phải từ 15 trở lên";
                return;
            }
            string userID = hdfID.Value;
            int ID = Convert.ToInt32(userID);
            string fullName = txtNameEdit.Text;
            string email = txtEmailEdit.Text;
            string gender = ddlGenderEdit.Text;
            int intGender = gender == "Nam" ? 1 : 0;

            
            string birthday = strMonth + "-" + strDate + "-" + strYear;

            string memberType= ddlMBTEdit.Text;
            string phoneNo= txtPhoneNoEdit.Text;
            string add = txtAddEdit.Text;

            sp.UpdateUser(ID, email, fullName, intGender, birthday, memberType, phoneNo, add);
            lblMs.Text = "Cập nhật thông tin thành công";
        }
    }
}